using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class BtnEventExample : MonoBehaviour
{
    public CanvasGroup muyuGroup;
    public Animator muyuAnim;
    public CanvasGroup tipGroup;
    public RectTransform tipRectt;
    private RectTransform selfRectt;
    public Vector2 oriPos;

    public AudioSource audioSource;

    private void Awake()
    {
        selfRectt = GetComponent<RectTransform>();
    }

    private void Start()
    {
        muyuGroup.alpha = 0;
        tipGroup.alpha = 0;
    }

    private void Update()
    {
        if (tipGroup.alpha > 0) { }
    }

    public void OnMove(PointerEventData data)
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            selfRectt,
            data.position,
            null,
            out Vector2 handlePos
        );
        tipRectt.anchoredPosition = handlePos - oriPos;
        // tipRectt.anchoredPosition = handlePos;
        // tipRectt.anchoredPosition = handlePos;
        // tipRectt.anchoredPosition = data.position;
    }

    public void OnClick()
    {
        muyuAnim.SetTrigger("Click");
        audioSource.Play();
        //Debug.Log("Clicked");
    }

    public void OnShow()
    {
        StopAllCoroutines();
        StartCoroutine(Show());
    }

    public void OnHide()
    {
        StopAllCoroutines();
        StartCoroutine(Hide());
    }

    IEnumerator Hide()
    {
        while (muyuGroup.alpha > 0)
        {
            muyuGroup.alpha -= Time.deltaTime * 3;
            tipGroup.alpha -= Time.deltaTime * 3;
            yield return null;
        }
        muyuGroup.alpha = 0;
        tipGroup.alpha = 0;
    }

    IEnumerator Show()
    {
        while (muyuGroup.alpha < 1)
        {
            muyuGroup.alpha += Time.deltaTime * 3;
            tipGroup.alpha += Time.deltaTime * 3;
            yield return null;
        }
        muyuGroup.alpha = 1;
        tipGroup.alpha = 1;
    }
}
